<template>
  <addEditPadMessageView ref="addEditPadMessageView" />
  <addEditBigScreen ref="addEditBigScreen" />
  <addFileView ref="addFileView" />
  <div class="app-table">
    <div class="app-body">
      <el-form :inline="true" class="demo-form-inline" v-model="form">

        <el-form-item label="所在年级" :label-width="formLabelWidth" prop="gradeId" v-if="form.msgType != 5">
          <el-select style="width:180px;" clearable v-model="form.gradeId" autocomplete="off" @change="filterGradeId"
            placeholder="请选择所在年级">
            <el-option v-for="gradeId in gradeIds" :key="gradeId" :value="gradeId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所在班级" :label-width="formLabelWidth" prop="classId" v-if="form.msgType != 5">
          <el-select style="width:180px;" clearable v-model="form.classId" autocomplete="off" placeholder="请选择所在班级">
            <el-option v-for="item in classInfos" :key="item.classId" :value="item.classId"></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="教室名称">
          <el-input style="width:180px;" placeholder="请输入教室名称" v-model="form.msgId"></el-input>
        </el-form-item> -->
        <el-form-item label="发布人">
          <el-input style="width:180px;" placeholder="请输入发布人" v-model="form.createName"></el-input>
        </el-form-item>
        <el-form-item label="消息内容">
          <el-input style="width:180px;" placeholder="请输入消息内容" v-model="form.content"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button class="serach_btn" plain type="primary" @click="onSubmit" @keyup.enter="searchEnter">搜索</el-button>
          <el-button class="add_btn" type="success" plain @click="add">添加</el-button>
          <!-- <el-button type="warning" @click="exportExcel">导出报表</el-button> -->
          <!-- <el-button type="danger" @click="importExcel" -->
          <!-- >批量导入<el-icon class="el-icon--right"><Upload /></el-icon -->
          <!-- ></el-button> -->
        </el-form-item>
      </el-form>
      <el-table :data="list" :row-class-name="tableRowClassName">
        <el-table-column prop="id" label="id" width="90" />
        <el-table-column prop="detail" label="班级" v-if="form.msgType != 5" width="140">
          <template #default="scope">
            <span v-if="scope.row.gradeId > 0">
              高{{ scope.row.gradeId }}级({{ scope.row.classId }})班
            </span>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="classroomId" label="人员id"/> -->
        <!-- <el-table-column prop="msgLevel" label="消息级别"/> -->
        <!-- <el-table-column prop="msgType" label="消息类型" /> -->
        <!-- <el-table-column prop="msgMode" label="消息展示模式"/> -->
        <el-table-column prop="pushPage" label="消息展示页面" width="130px">
          <template #default="scope">
            {{ scope.row.pushPage == 1 ? '班牌首页' : scope.row.pushPage == 2 ? '紧急通知页'
        : scope.row.pushPage == 3 ? '设置页面' : scope.row.pushPage == 4 ? '显示状态栏' : scope.row.pushPage == 5 ? '隐藏状态栏' :
          scope.row.pushPage == 6 ? '版本升级' : scope.row.pushPage == 7 ? '重启设备' : scope.row.pushPage == 8 ? '设备截屏' : '无'
            }}
          </template>
        </el-table-column>

        <el-table-column prop="operationType" label="操作类型" width="80px">
          <template #default="scope">
            {{ scope.row.operationType == 2 ? '追加' : '新建' }}
          </template>

        </el-table-column>
        <!-- <el-table-column prop="padNumber" label="设备号"/> -->
        <!-- <el-table-column prop="imageUrls" label="图片urls"/> -->
        <!-- <el-table-column prop="videoUrl" label="视频url"/> -->
        <el-table-column prop="content" label="消息内容" min-width="200" />
        <!-- <el-table-column prop="showStartTime" label="开始展示时间"/> -->
        <el-table-column prop="showEndTime" label="结束展示时间" width="170" />
        <el-table-column prop="createTime" label="发布时间" width="170" />
        <el-table-column prop="createName" label="发布人" width="80px" />
        <!-- <el-table-column prop="createNameNumber" label="发布人工号"/> -->
        <!-- <el-table-column prop="isDelete" label="是否删除、管理员能看见删除的"/> -->
        <!-- <el-table-column prop="campus" label="校区"/> -->
        <el-table-column prop="msgId" label="教室名称" />
        <el-table-column label="操作" width="140px">
          <template style="display: block" #default="scope">
            <!-- <el-button
              size="small"
              type="success"
              @click="editPadMessage(scope.row)"
              >编辑
            </el-button> -->
            <el-button size="small" type="danger" v-if="scope.row.isDelete == 0"
              @click="deletePadMessage(scope.row.id)">撤销
            </el-button>
            <el-button size="small" type="danger" v-if="scope.row.isDelete == 1"
              @click="deletePadMessageReal(scope.row.id)">彻底删除
            </el-button>
          </template>
        </el-table-column>
        <template v-slot:empty>
          <el-empty description="暂无数据"></el-empty>
        </template>
      </el-table>
      <el-config-provider :locale="locale">
        <el-pagination :total="total" :page-size="pageSize" :current-page="page"
          :page-sizes="[5, 10, 20, 30, 40, 50, 100]" layout="total, sizes, prev, pager, next, jumper"
          @size-change="changePageSize" @current-change="changeCurrentPage">
        </el-pagination>
      </el-config-provider>
    </div>
    <bottomView></bottomView>

  </div>
</template>
<script>
import bottomView from "@/components/bottom.vue";

import addEditPadMessageView from "@/components/pad/addEditPadMessage.vue";
import addEditBigScreen from "@/components/pad/addEditBigScreen.vue";
import addFileView from "@/components/addExcelFile.vue";
import { ElConfigProvider, ElMessageBox, ElMessage } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { httpService } from "@/utils/httpService";
import { Picture as IconPicture, Upload } from "@element-plus/icons-vue";

export default {
  name: "PadMessageListView",
  data() {
    return {
      type: 0,
      title: "",
      page: 1,
      pageSize: 10,
      total: 0,
      // limit: 10,
      list: [],
      map: {},
      classInfos: [],
      gradeIds: [],
      form: {
        padNumber: "",
        imageUrls: "",
        content: "",
        videoUrl: "",
        createName: "",
        createNameNumber: "",
        msgId: "",
        classId: "",
        gradeId: "",
        msgType: "",
        isDelete: 0
      },
    };
  },
  components: {
    bottomView,
    addEditPadMessageView,
    addFileView,
    IconPicture,
    ElConfigProvider,
    ElMessageBox,
    ElMessage,
    Upload, addEditBigScreen
  },
  setup() {
    return {
      locale: zhCn,
    };
  },
  mounted() {
    this.getData()
    this.getGradeIds();
    this.getPadMessageList();
  },
  watch: {
    $route() {
      this.getData();
      this.getGradeIds();
      this.getPadMessageList();
    },
  },
  methods: {
    getBaseClassInfoList() {
      httpService("/javaApi/baseClassInfo/getAllEffectClass", {}, "get").then(
        (data) => {
          this.map = data;
        }
      );
    },
    filterGradeId(val) {
      this.classInfos = this.map[val];
    },
    getGradeIds() {
      let date = new Date();
      let nowYear = parseInt(date.getFullYear());
      let nowMonth = parseInt(date.getMonth());
      let gradeIds = [];
      if (nowMonth > 6) {
        gradeIds.push(nowYear - 2);
        gradeIds.push(nowYear - 1);
        gradeIds.push(nowYear);
      } else {
        gradeIds.push(nowYear - 3);
        gradeIds.push(nowYear - 2);
        gradeIds.push(nowYear - 1);
      }
      this.gradeIds = gradeIds;
      this.getBaseClassInfoList();
    },
    getData() {
      if (this.$route.query.msgType) {
        this.form.msgType = this.$route.query.msgType;
      } else {
        this.form.msgType = "";
      }
      if (this.$route.query.d) {
        this.form.isDelete = this.$route.query.d;
      } else {
        this.form.isDelete = 0;
      }
    },
    searchEnter: function (e) {
      // 使用 which 和 keyCode 属性来解决兼容问题
      var keyCode = window.event ? e.keyCode : e.which;
      var val = e.target.value;
      if (keyCode == 13 && val) {
        this.page = 1
        this.getPadMessageList();
      }
    },
    add() {
      if (this.form.msgType == 2) {
        this.$refs.addEditPadMessageView.title = "添加班级消息";
      } else
        if (this.form.msgType == 3) {
          this.$refs.addEditPadMessageView.title = "添加级部消息";
          this.$refs.addEditPadMessageView.getBaseDepartmentList();
        } else
          if (this.form.msgType == 4) {
            this.$refs.addEditPadMessageView.title = "添加年级消息";
          }
          else
            if (this.form.msgType == 1) {
              this.$refs.addEditPadMessageView.title = "添加全校消息";
            }
            else
              if (this.form.msgType == 5) {
                // this.$refs.addEditPadMessageView.title = "添加设备消息";
                // this.$refs.addEditPadMessageView.getNRoomList();
                this.$refs.addEditBigScreen.title = "添加设备消息";
                this.$refs.addEditBigScreen.getNRoomList();
                this.$refs.addEditBigScreen.addPadMessageForm.msgType = this.form.msgType;
                this.$refs.addEditBigScreen.dialogFormVisible = true;
                return
              }
      this.$refs.addEditPadMessageView.addPadMessageForm.msgType = this.form.msgType;
      this.$refs.addEditPadMessageView.getGradeIds();
      this.$refs.addEditPadMessageView.getBaseClassInfoList();
      this.$refs.addEditPadMessageView.dialogFormVisible = true;
    },
    editPadMessage(obj) {
      this.$refs.addEditPadMessageView.title = "修改信息";
      this.$refs.addEditPadMessageView.getPadMessageById(obj.id);
      this.$refs.addEditPadMessageView.dialogFormVisible = true;
    },
    deletePadMessage(id) {
      ElMessageBox.confirm("确定要删除本条记录吗？", "提醒", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          httpService(
            "/javaApi/padMessage/updateById",
            "id=" + id,
            "post"
          ).then((data) => {
            if (data.code == 200) {
              ElMessage.success("撤销成功");
              this.getPadMessageList();
            } else {
              ElMessage.error("撤销失败，请重试！");
            }
          });
        })
        .catch(() => { });
    },
    deletePadMessageReal(id) {
      ElMessageBox.confirm("确定要删除本条记录吗？", "提醒", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          httpService(
            "/javaApi/padMessage/deleteById",
            "id=" + id,
            "post"
          ).then((data) => {
            if (data.code == 200) {
              ElMessage.success("删除成功");
              this.getPadMessageList();
            } else {
              ElMessage.error("删除失败，请重试！");
            }
          });
        })
        .catch(() => { });
    },
    reloadData() {
      this.page = 1;
      this.getPadMessageList();
    },
    importExcel() {
      this.$refs.addFileView.dialogFormVisible = true;
      this.$refs.addFileView.title = "批量导入";
      this.$refs.addFileView.uploadApiUrl = "/javaApi/padMessage/importExcel";
    },
    exportExcel() {
      httpService(
        "/javaApi/padMessage/exportExcel",
        {
          padNumber: this.form.padNumber,
          imageUrls: this.form.imageUrls,
          videoUrl: this.form.videoUrl,
          createName: this.form.createName,
          createNameNumber: this.form.createNameNumber,
          msgId: this.form.msgId,
          content: this.form.content,
          msgType: this.form.msgType,
          isDelete: this.form.isDelete

        },
        "get"
      ).then((data) => {
        if (data.code == 200) {
          window.open(data.msg, "_blank");
        }
      });
    },
    onSubmit() {
      this.page = 1
      this.getPadMessageList();
    },
    // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
    changePageSize(size) {
      this.pageSize = size;
      // this.limit = size;
      this.getPadMessageList();
    },

    // 改变页码，page：回调参数，表示当前选中的“页码”
    changeCurrentPage(page) {
      this.page = page;
      this.getPadMessageList();
    },
    tableRowClassName({ row, rowIndex }) {
      return "";
    },
    getPadMessageList() {
      console.log("msgType=", this.form.msgType);
      httpService(
        "/javaApi/padMessage/list/" + this.page + "/" + this.pageSize,
        {
          padNumber: this.form.padNumber,
          imageUrls: this.form.imageUrls,
          videoUrl: this.form.videoUrl,
          createName: this.form.createName,
          createNameNumber: this.form.createNameNumber,
          msgId: this.form.msgId,
          msgType: this.form.msgType,
          content: this.form.content,
          gradeId: this.form.gradeId ? this.form.gradeId : "",
          classId: this.form.classId ? this.form.classId : "",
          isDelete: this.form.isDelete

        },
        "get"
      ).then((data) => {
        this.list = data.list;
        this.total = data.total;
        this.pageCount = data.pages;
      });
    },
  },
};
</script>
<style scoped>
.el-main {
  padding: 0px;
}


.app-table {
  /* width: 100%; */
  /* width: calc(100% - 40px); */
  background: #eff3f5;
  padding: 10px;
}

.app-separator {
  padding-top: 10px;
  padding-bottom: 20px;
  padding-left: 10px;
}

.app-body {
  min-height: calc(100vh - 243px);
  background: white;
  padding: 20px 20px 10px 20px;
  margin-bottom: 10px;
}

.el-table .warning-row {
  --el-table-tr-bg-color: var(--el-color-warning-lighter);
}

.el-table .success-row {
  --el-table-tr-bg-color: var(--el-color-success-lighter);
}

.el-pagination {
  text-align: center;
  margin-top: 10px;
}

.add_btn {
  background-color: #00c78c;
  color: #fff;
  border-radius: 8px;
  border: none;
}

.serach_btn {
  background-color: #007EFF;
  color: #fff;
  border-radius: 8px;
  border: none;
}
</style>
